/* 
 * File:   LoginController.cpp
 * Author: edwin
 *
 * 
 */
#include "LoginController.h"

 LoginController::LoginController()
 {

 }


 User LoginController::run()
 {

 	readUserData();
 	User tmp;

 	bool isAuthenticated = false;

 	string input_name;
 	string input_password;
 	int role_id;

 	//for debugging
 	showUserData();

 	do {

 		input_name = "";
 		input_password = "";

 		cout << "Please enter your username: ";
 		cin >> input_name;
 		cin.ignore();
 		cout << "Please enter your password: ";
 		cin >> input_password;
 		cin.ignore();

 		isAuthenticated = validateUser(input_name, input_password);

 	} while(!isAuthenticated);

 	if (isAuthenticated) {
 		for(vector<User>::iterator it = user_list.begin(); it != user_list.end(); ++it) {
 			if( (*it).getUserName() == input_name ) {
 				role_id = atoi(((*it).getUserRole()).c_str());
 				tmp = (*it);
 			}
 		}
 	}

 	return tmp;

/* 	for(vector<Package>::iterator it = package_list.begin(); it != package_list.end(); ++it) {
 		cout << (*it).getPackId() << " . " << (*it).getPackName() << " . "
 		<< (*it).getPackDuration() << endl; 
 	}*/

 	//for debugging
 	//cout << "Role is" << role_id << endl;
 }

 void LoginController::readUserData()
 {
 	vector<string> usr_str;
 	string tmp_str = "";
 	vector<string> vec_str;

 	ifstream userFile("USER.dat", ios::in);

 	if(userFile.is_open()) {
 		while(userFile.good()) {
 			getline(userFile, tmp_str);
 			if(tmp_str == "") {
 				break;
 			}

 			usr_str.push_back(tmp_str);

 		}
 	} else {
 		cout << "Error opening user file" << endl;
 	}

 	for(vector<int>::size_type i = 0; i != usr_str.size(); i++) {
 		stringstream ss(usr_str[i]);
 		tmp_str = "";

 		vec_str.clear();
 		while(getline(ss, tmp_str, ',')) {
 			vec_str.push_back(tmp_str);
 		}

 		user_list.push_back(User(vec_str[0], vec_str[1], vec_str[2], vec_str[3]));

 	}

 	userFile.close();

 }

 void LoginController::writeUserData()
 {
 	ofstream fileStream("USER.dat", ios::out);

 	if (fileStream.is_open()) {

 		for(vector<int>::size_type i = 0; i < user_list.size(); i++) {
 			fileStream << user_list[i].getUserId() << "," << user_list[i].getUserName() 
 			<< "," << user_list[i].getUserPassword() << "," << user_list[i].getUserRole() << endl;
 		}

 	} else {
 		cout << "Unable to open data file" << endl;
 	}

 	fileStream.close();
 }

 void LoginController::showUserData()
 {
 	for(vector<User>::iterator it = user_list.begin(); it != user_list.end(); ++it) {
 		cout << (*it).getUserName() << " . " << (*it).getUserPassword() << " . "
 		<< (*it).getUserId() << " . " << (*it).getUserRole() << endl;
 	}
 }

 bool LoginController::validateUser(string input_name, string input_password)
 {
 	bool userAuthenticated, userCorrect, passwordCorrect = false;

 	for(vector<User>::iterator it = user_list.begin(); it != user_list.end(); ++it) {
 		if( (*it).getUserName() == input_name ) {
 			userCorrect = true;
 			if( (*it).getUserPassword() == input_password ) {
 				passwordCorrect = true;
 			}
 		}
 	}

 	if ( userCorrect && passwordCorrect ) {
 		userAuthenticated = true;
 	}

 	return userAuthenticated;
 }


void LoginController::addNewUser()
{
	string tmp = "";
	int tmp_packid = user_list.size();
	tmp_packid++;

	string newId = "" + tmp_packid;;
	//newId = newId + 

	char role;

	cout << "********** Add New Staff Account **********" << endl;
	User newUser;
	cout << "Enter New Staff Username: ";
	cin >> tmp;
	cin.ignore();
	newUser.setUserName(tmp);

	cout << "Enter New Staff Password: ";
	cin >> tmp;
	cin.ignore();
	newUser.setUserPassword(tmp);

	newUser.setUserId("888");

	cout << "Select New Staff Role" << endl;
	cout << "1. General Manager" << endl;
	cout << "2. Holiday Manager" << endl;
	cout << "3. Booking Staff" << endl;
	cout << "4. Finance Officer" << endl;
	cout << "Option: ";

	cin >> role;
	cin.ignore();

	switch (role) {
		case '1':
			newUser.setUserRole("1");
			break;
		case '2':
			newUser.setUserRole("2");
			break;
		case '3':
			newUser.setUserRole("3");
			break;
		case '4':
			newUser.setUserRole("4");
			break;
		default:
			break;
	}

	user_list.push_back(newUser);
}

void LoginController::editExistingUser()
{
	string usr_input = "";
	bool found = false;
	cout << "*********** Edit Existing Staff Account **********" << endl;
	cout << "Enter Name of Staff: ";
	//cin.ignore();
	getline(cin, usr_input);

	for(vector<User>::iterator it = user_list.begin(); it != user_list.end(); ++it) {
		if( (*it).getUserName() == usr_input ) {
			found = true;

		}
	}

	if(found) {
		string newName = "";
		string newPassword = "";
		cout << "Enter New UserName of Staff: ";
		getline(cin,newName);
		cout << "Enter New Password of Staff: ";
		getline(cin,newPassword);

		for(vector<User>::iterator it = user_list.begin(); it != user_list.end(); ++it) {
			if( (*it).getUserName() == usr_input ) {
				(*it).setUserName(newName);
				(*it).setUserPassword(newPassword);
			}
		}
	} else {
		cout << "The User is not Found" << endl;
	}
}

void LoginController::deleteExistingUser()
{
	string pDelete = "";
	cout << "********* Delete Existing Staff Account *********" << endl;
	cout << "Enter Name of Staff: ";
	getline(cin, pDelete);

	for(vector<User>::iterator it = user_list.begin(); it != user_list.end(); ++it) {
		if( (*it).getUserName() == pDelete ) {
			user_list.erase(it);
			cout << pDelete << " removed" << endl;
			break;
		}
	}
}

void LoginController::addNewClient()
{
	string tmp = "";
	int tmp_packid = user_list.size();
	tmp_packid++;

	string newId = "" + tmp_packid;;
	//newId = newId + 

	char role;

	cout << "********** Add New Client **********" << endl;
	User newUser;
	cout << "Enter New Staff Username: ";
	getline(cin, tmp);
	newUser.setUserName(tmp);

	cout << "Enter New Staff Password: ";
	getline(cin, tmp);
	newUser.setUserPassword(tmp);

	newUser.setUserId("888");

	newUser.setUserRole("5");

	user_list.push_back(newUser);
}

void LoginController::editExistingClient()
{
	string usr_input = "";
	bool found = false;
	cout << "*********** Edit Existing Client Account **********" << endl;
	cout << "Enter Name of Client: ";
	//cin.ignore();
	getline(cin, usr_input);

	for(vector<User>::iterator it = user_list.begin(); it != user_list.end(); ++it) {
		if( (*it).getUserName() == usr_input ) {
			found = true;

		}
	}

	if(found) {
		string newName = "";
		string newPassword = "";
		cout << "Enter New UserName of Client: ";
		getline(cin,newName);
		cout << "Enter New Password of Client: ";
		getline(cin,newPassword);

		for(vector<User>::iterator it = user_list.begin(); it != user_list.end(); ++it) {
			if( (*it).getUserName() == usr_input ) {
				(*it).setUserName(newName);
				(*it).setUserPassword(newPassword);
			}
		}
	} else {
		cout << "The User is not Found" << endl;
	}
}

void LoginController::deleteExistingClient()
{
	string pDelete = "";
	cout << "********* Delete Existing Client Account *********" << endl;
	cout << "Enter Name of Staff: ";
	getline(cin, pDelete);

	for(vector<User>::iterator it = user_list.begin(); it != user_list.end(); ++it) {
		if( (*it).getUserName() == pDelete ) {
			user_list.erase(it);
			cout << pDelete << " removed" << endl;
			break;
		}
	}
}
